var hashTable = require('../common/hashtable.js');

var MODNAME = 'SessionManager';

var SessionManager = function(pAppObj) {
	
	this._Sessions = hashTable.create();
	this.logger = pAppObj.XLOGGER;
	
};

SessionManager.prototype.close = function () {
	this._Sessions.clear();
};


/**
 * Get SsnTable object
 * @param pFsmObj	Object, FsmObject
 */
function getSessionTable(pFsmObj) {
	var gAppData = pFsmObj.fsmManager.getApplicationDataObj();
	return gAppData.appSessionTable;
}

/**
 * Creates new Session
 * @param pFsmObj
 * @param pSessionId
 * @returns newSessionObject
 */
function createSession(pFsmObj, pSessionId) {
	var appSessionTable = getSessionTable(pFsmObj);
	var newSessionObj = getNewRecordObj();
	if(newSessionObj) {
		newSessionObj["Session-Id"] = pSessionId;
	}
	appSessionTable.setItem(pSessionId, newSessionObj);
	
	return newSessionObj;
}

/**
 * delete the session
 * @param pFsmObj
 * @param pSessionId
 */
function deleteSession(pFsmObj, pSessionId) {
	var appSessionTable = getSessionTable(pFsmObj);
	appSessionTable.removeItem(pSessionId);
}

/**
 *  Get the data object of given sessionid
 * @param pFsmObj
 * @param pSessionId
 * @returns	SessionObject
 */
function getSessionData(pFsmObj, pSessionId) {
	var appSessionTable = getSessionTable(pFsmObj);
	var ssnObj = appSessionTable.getItem(pSessionId);
	
	return ssnObj;
}

/**
 * Is used to retrieve the sdpsessionobject
 * @param pFsmSession		Object,	FsmSessionObject
 * @param pSdpSessionId		String,	SdpSessionId
 * @returns					Object,	SdpSessionObject
 */
function getSdpSessionObject(pFsmSession, pSdpSessionId) {
	console.log("Current Objects in Session Table");
	console.log(JSON.stringify(getSessionTable(pFsmSession), null, "\t"));
	
	// Retrieve Session Object
	var sdpSessionObj = undefined;
	sdpSessionObj = getSessionData(pFsmSession, pSdpSessionId);
	// send err message, if couldn't retrieve session object
	if (sdpSessionObj === undefined) {
		//throw new Error(sdseDefines.fnGetMsg(104));
	}
	
	console.log("Got session object");
	console.log(JSON.stringify(sdpSessionObj, null, "\t"));
	
	return sdpSessionObj;
}

/*
Record Structure for entries in the sessionTable
*/
/*
sessionRecord = function() {};
sessionRecord.prototype = {
	"Session-Id" 				: "0",	
	"Subscriber-Id" 			: "0",
	"Subscriber-CustomerId" 	: "0",	
	"Subscriber-Account-Id"		: "0",
	"Subscriber-Location-Id"	: "0",
	
	"Receiver-Id" 	: "0",
	"Receiver-CustomerId" 		: "0",
	"Receiver-Account-Id"		: "0",
	"Receiver-Location-Id"		: "0",
	
	"IsAuthenticated" 			: "0",	
	"RequestId"					: "0"
};
*/

/* This function creates a child object that is inherited from the parent's prototype */
function inherit(p) {
    if (p === null) throw TypeError(); // p must be a non-null object
    if (Object.create) {              // If Object.create() is defined...
       return Object.create(p);      //    then just use it.
    }
    var t = typeof p;                 // Otherwise do some more type checking
    if (t !== "object" && t !== "function") {
    	throw new TypeError();
    }
    function f() {}                 // Define a dummy constructor function.
    f.prototype = p;                  // Set its prototype property to p.
    return new f();                   // Use f() to create an "heir" of p.
}

function getNewRecordObj() {
	// Set its prototype property to p.
	
	var sessionRecord = {};	
	
	sessionRecord['Session-Id'] = '0';	
	
	sessionRecord['ssnRequestId'] = '0';
	sessionRecord['ssnSender-Info'] = '0';
	sessionRecord['ssnReceiver-Info'] = '0';
	sessionRecord['ssnService-Information'] = '0';
	
	sessionRecord['IsAuthenticated'] = '0'; 
	sessionRecord['IsChargingRqstRcvd'] = '0'; 
		
	/*
	sessionRecord['Subscriber-Id'] = '0';
	sessionRecord['Subscriber-CustomerId'] = '0';	
	sessionRecord['Subscriber-Account-Id'] = '0';
	sessionRecord['Subscriber-Location-Id'] = '0';		
	sessionRecord['Receiver-Id']	= '0';
	sessionRecord['Receiver-CustomerId'] = '0';
    sessionRecord['Receiver-Account-Id'] = '0';
    sessionRecord['Receiver-Location-Id'] = '0';
	*/
	
	//console.log(obj["Subscriber-CustomerId"]);
	return sessionRecord;
}

exports.create = function(pObj) { return new SessionManager(pObj); };
